package com.smartEdu.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.smartEdu.commons.ClassStatic;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.utils.XmlUtil;

public class InitServerListener implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		String path = arg0.getServletContext().getRealPath("/");
		System.setProperty("WORKDIR", path);
		initSystemconfig();
		Tag_Tips.PRVATEKEY=XmlUtil.getContent("RSA.PrivateKey");
	}
	/**
	 * 获取SystemConfig的xml文件配置信息
	 */
	public void initSystemconfig(){
		File file=null;
		InputStream is=null;
		try {
			file=new File(System.getProperty("WORKDIR")+"config/SystemConfig.xml");
			is=new FileInputStream(file);
			ClassStatic.docSystem=XmlUtil.fromXML(is, "");
			LogCvt.debug("加载SystemConfig.xml成功");
		} 
		catch (Exception e) {
			LogCvt.error("加载SystemConfig.xml错误",e);
		}
		finally {
			if (is!=null) {
				try {
					is.close();
				} catch (IOException e) {
					LogCvt.error("FileInputStream关闭异常",e);
				}
			}
		}
	}
}
